package com.luych.common.util;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static final Calendar calendar = Calendar.getInstance();

    /**
     * 获取当前时间的时间戳
     *
     * @return 返回处理结果
     */
    public static Timestamp getNow() {
        Date date = new Date();
        return new Timestamp(date.getTime());
    }

    /**
     * 处理日期的增减
     *
     * @param date  源日期
     * @param field 字段
     * @param value 差值
     * @return 返回处理结果
     */
    public static Date dateAdd(Date date, int field, int value) {
        calendar.setTime(date);
        calendar.add(field, value);
        return calendar.getTime();
    }

    /**
     * 获取当前日志的0点0分
     *
     * @param date 日期
     * @return 返回处理结果
     */
    public static Date getDateBegin(Date date) {
        calendar.setTime(date);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取月初
     *
     * @param date 日期
     * @return 返回处理结果
     */
    public static Date getMonthBegin(Date date) {
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return getDateBegin(calendar.getTime());
    }

    /**
     * 获取月末
     *
     * @param date 日期
     * @return 返回处理结果
     */
    public static Date getMonthEnd(Date date) {
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        date = calendar.getTime();
        date = getMonthBegin(date);
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

}
